//配置公用方法
import _ from "lodash-es"
/**
 * 根据附件路径生成一个对象
 * url: 文件路径  prev 文件服务器地址
 */
export const getFileObj = (url, prev, fileName) => {
  return {
    uid: _.uniqueId(),
    name: fileName || "default",
    status: "done",
    url: prev + url,
    privateUrl: url,
  }
}

/**
 * 生成一个随机id
 */
export const randomId = () => {
  return Number(
    Math.random()
      .toString()
      .substr(3, length) + Date.now()
  ).toString(36)
}

//更新滚动条
export const updataScroll = () => {
  const updateFnObj = window.updateFnObj
  Object.keys(updateFnObj).forEach((key) => {
    if (typeof updateFnObj[key] !== "function") return
    updateFnObj[key]()
  })
}

//把要分组的字段提取出来存放在一个数组然后去重，然后在循环分组
export const groupArr = (list, field) => {
  var fieldList = [],
    att = []
  list.map((e) => {
    fieldList.push(e[field])
  })
  //数组去重
  fieldList = fieldList.filter((e, i, self) => {
    return self.indexOf(e) == i
  })
  for (var j = 0; j < fieldList.length; j++) {
    //过滤出匹配到的数据
    var arr = list.filter((e) => {
      return e[field] == fieldList[j]
    })
    att.push({
      [field]: arr[0][field],
      list: arr,
    })
  }
  return att
}

//获取当前时间 (机器人)
export const nowTime = () => {
  let now = new Date()
  let _month =
    10 > now.getMonth() + 1 ? "0" + (now.getMonth() + 1) : now.getMonth() + 1
  let _day = 10 > now.getDate() ? "0" + now.getDate() : now.getDate()
  let _hour = 10 > now.getHours() ? "0" + now.getHours() : now.getHours()
  let _minute =
    10 > now.getMinutes() ? "0" + now.getMinutes() : now.getMinutes()
  let _second =
    10 > now.getSeconds() ? "0" + now.getSeconds() : now.getSeconds()
  return (
    // now.getFullYear() +
    // "-" +
    // _month +
    // "-" +
    // _day +
    // " " +
    _hour + ":" + _minute
    // +
    // ":" +
    // _second
  )
}

/**生成32位uuid */
export const getUUID = (n = 32) => {
  var str = "abcdefghijklmnopqrstuvwxyz0123456789" // 可以作为常量放到random外面
  var result = ""
  for (var i = 0; i < n; i++) {
    result += str[parseInt(Math.random() * str.length)]
  }
  return result
}

/**
 * 对象转array
 * @param {*} object
 * @returns Array
 */
 export const objToArray = (object) => {
  let keys = Object.keys(object).reverse()
  let objArray = keys.map((key) => {
    return {
      key: key,
      value: object[key].map(item => {
        if(item === '小通正在学习这方面的知识呢~') {
          return '安信助手正在学习这方面的知识呢~'
        }else if(item === '小通不明白您问的问题，您可以尝试其他问题~') {
          return '安信助手不明白您问的问题，您可以尝试其他问题~'
        }
        return item
      }),
    }
  })
  return objArray
}
/**
 * 对象转换map
 * @param {*} object
 * @returns Map
 */
 export function objToMap(obj) {
  let result = Object.entries(obj)
  return new Map(result)
}

/**
 * 字符串对象转对象
 * @param {*} object
 * @returns Obj
 */
 export function parseJsonString(jsonString){
  jsonString = jsonString || '{}';
  var jsonObj;
  try{
   jsonObj = typeof jsonString == 'object' ? jsonString : JSON.parse(jsonString);
  }catch(err){
   jsonObj = {}
  } 
   return jsonObj;
}